package com.codefox.service;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codefox.entity.TbUser;
import com.codefox.mapper.TbUserMapper;
import com.codefox.util.PageUtil;

/**
 * 用户信息服务层
 * 
 * @author lt
 * @author xiaobiye
 * @author puma
 * @date 2018年9月26日14:45:25 2018年9月30日10:22:21
 */
@Service
public class TbUserService {

	@Autowired
	SqlSessionFactory factory;
	@Autowired
	PageUtil<TbUser> pageUtil;
	/**
	 * 分页
	 */
	public PageUtil<TbUser> getUserPage(int pageIndex,int pageSize){
		SqlSession sqlSession = null;
		List<TbUser> list = null;
		int count = 0;//总记录数
		try {
			sqlSession = factory.openSession();
			TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);
			count = mapper.getCount();
			//
			Map<String, Integer> map = new HashMap<>();
			map.put("begin", (pageIndex-1)*pageSize);
			map.put("end", pageSize-1);
			list = mapper.search(map);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		//封装分页数据
		pageUtil.setPageSize(pageSize);
		pageUtil.setPageIndex(pageIndex);
		pageUtil.setCount(count);
		pageUtil.setPageNumber(count%pageSize==0?count/pageSize:count/pageSize+1);
		pageUtil.setList(list);
		return pageUtil;
		
	}
	
	
	/**
	 * 获得所有用户信息
	 * 
	 * @return 用户信息List集合
	 */
	public List<TbUser> userFind() {
		// 此对象依赖SqlSessionFactory工厂。工厂在Spring的配置文件已经配置，依赖注入就行了。
		SqlSession sqlSession = null;
		List<TbUser> list = null;
		try {
			sqlSession = factory.openSession();
			// 应用 SqlSession操作数据。取数据
			TbUserMapper dao = sqlSession.getMapper(TbUserMapper.class);
			list = dao.userFind();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return list;
	}

	/**
	 * 登录服务层
	 * 
	 * @param username
	 *            页面的用户名
	 * @param password
	 *            页面的密码
	 * @param message
	 *            用户返回错误消息
	 * @return
	 */
	public TbUser login(String username, String password, StringBuffer message) {
		TbUser user;

		try (SqlSession sqlSession = factory.openSession()) {
			TbUserMapper dao = sqlSession.getMapper(TbUserMapper.class);
			user = dao.selectByName(username);
		}

		if (user == null) {
			message.append("用户名错误");
			System.out.println("login3:" + message);
			return user;
		} else {
			if (user.getPassword().equals(password)) {
				if ("1".equals(user.getStatus()))
					return user;
				else
					message.append("该账户未经过审核");
				System.out.println("login3:" + message);
			} else
				message.append("密码错误");
			System.out.println("login3:" + message);
		}
		return null;
	}

	/**
	 * 用于ajax 验证用户名是否存在
	 * 
	 * @param username
	 * @return
	 */
	public String checkUserName(String username) {
		SqlSession sqlSession = null;
		TbUser user = null;
		System.out.println("checkUserName-USERNAME:" + username);
		try {
			sqlSession = factory.openSession();
			TbUserMapper dao = sqlSession.getMapper(TbUserMapper.class);
			user = dao.selectByName(username);
			System.out.println("username1::" + username);
			System.out.println("user2::" + user);
			if (user != null) {
				return "y";
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return "n";
	}

	public boolean register(TbUser user) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);
			int count = mapper.insertSelective(user);
			if (count > 0) {
				return true;
			} else {
				return false;
			}
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}

	public TbUser findById(int id) {
		SqlSession sqlSession = null;
		TbUser tbUser = null;
		try {
			sqlSession = factory.openSession();
			TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);
			tbUser = mapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return tbUser;
	}

	@Transactional
	public int update(TbUser tbUser) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);
			if (tbUser.getUserId()!=null) {
				return mapper.updateByPrimaryKeySelective(tbUser);
			}else{
				return mapper.insertSelective(tbUser);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return 0;
	}

	@Transactional
	public int delete(Integer id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);
			return mapper.deleteById(id);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return 0;
	}
}
